package com.uum.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uum.admin.pojo.entity.SysMenuRole;
import com.uum.admin.pojo.entity.SysPermissionRole;
import com.uum.admin.pojo.entity.SysRole;
import com.uum.admin.pojo.entity.SysUserRole;
import com.uum.admin.mapper.SysRoleMapper;
import com.uum.admin.service.SysMenuRoleService;
import com.uum.admin.service.SysPermissionRoleService;
import com.uum.admin.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uum.admin.service.SysUserRoleService;
import com.uum.web.exception.BizException;
import com.uum.web.util.JwtUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Tsang_yc
 * @since 2022-01-18
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    /**
     * 从token中获取角色的ID
     *
     * @return
     */
    @Override
    public List<Long> getRoleIds() {
        //从token中获取角色
        List<String> roles = JwtUtils.getRoles();
        //根据角色编码获取角色ID
        List<Long> roleIdList = baseMapper.selectList(
                new LambdaQueryWrapper<SysRole>().in(SysRole::getCode, roles).select(SysRole::getRoleId)
        ).stream().map(SysRole::getRoleId).collect(Collectors.toList());
        if (CollUtil.isEmpty(roleIdList)) {
            throw new BizException("用户角色异常");
        }
        return roleIdList;
    }

}
